package com.sinog.biz.config;

import com.sinog.biz.util.DesCryptoGraphy;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.util.Objects;

/**
 * @desc 数据源配置
 * @author lzq
 * @date -
 */
@Configuration
public class DasourceConfig {

    /**
     * 全量数据交互数据库配置
     */
    @Autowired
    private DataBase1Properties dataBase1Properties;
    /**
     * 部级数据库配置
     */
    @Autowired
    private DataBase2Properties dataBase2Properties;
    /**
     * 本地数据库配置
     */
    @Autowired
    private DataBase3Properties dataBase3Properties;
    /**
     * 预安置人员数据录入中间库数据库配置
     */
    @Autowired
    private DataBase4Properties dataBase4Properties;
    /**
     * 自建省份同步
     */
    @Autowired
    private DataBase5Properties dataBase5Properties;
    /**
     * 预安置同步中间库数据源
     */
    @Autowired
    private DataBase6Properties dataBase6Properties;
    /**
     * 预安置同步中间库oracle数据源
     */
    @Autowired
    private DataBase7Properties dataBase7Properties;

    /**
     * 本地库数据源
     * @return DataSource
     */
    @Bean(name = "dataSource")
    @Primary
    public DataSource dataSource() {
        HikariDataSource hds = new HikariDataSource();
        String str = dataBase3Properties.getPassword();
        byte[] bytes = DesCryptoGraphy.hexStringToByte(str);
        byte[] pwdb = DesCryptoGraphy.desCbcDecrypt(bytes,DesCryptoGraphy.KEY.getBytes());
        hds.setDriverClassName(dataBase3Properties.getDriverClassName());
        hds.setJdbcUrl(dataBase3Properties.getUrl());
        hds.setUsername(dataBase3Properties.getUsername());
        hds.setPassword(new String(Objects.requireNonNull(pwdb)));
        hds.setMinimumIdle(dataBase3Properties.getMinimumIdle());
        hds.setMaximumPoolSize(dataBase3Properties.getMaximumPoolSize());
        hds.setAutoCommit(dataBase3Properties.isAutoCommit());
        hds.setIdleTimeout(dataBase3Properties.getIdleTimeout());
        hds.setPoolName(dataBase3Properties.getPoolName());
        hds.setMaxLifetime(dataBase3Properties.getMaxLifetime());
        hds.setConnectionTimeout(dataBase3Properties.getConnectionTimeout());
        hds.setConnectionTestQuery(dataBase3Properties.getConnectionTestQuery());
        return hds;
    }

    /**
     * 全量数据交互中间库数据源
     * @return DataSource
     */
    @Bean(name = "middleDataSource")
    public DataSource middleDataSource() {
        HikariDataSource hds = new HikariDataSource();
        String str = dataBase1Properties.getPassword();
        byte[] bytes = DesCryptoGraphy.hexStringToByte(str);
        byte[] pwdb = DesCryptoGraphy.desCbcDecrypt(bytes,DesCryptoGraphy.KEY.getBytes());
        hds.setDriverClassName(dataBase1Properties.getDriverClassName());
        hds.setJdbcUrl(dataBase1Properties.getUrl());
        hds.setUsername(dataBase1Properties.getUsername());
        hds.setPassword(new String(Objects.requireNonNull(pwdb)));
        hds.setMinimumIdle(dataBase1Properties.getMinimumIdle());
        hds.setMaximumPoolSize(dataBase1Properties.getMaximumPoolSize());
        hds.setAutoCommit(dataBase1Properties.isAutoCommit());
        hds.setIdleTimeout(dataBase1Properties.getIdleTimeout());
        hds.setPoolName(dataBase1Properties.getPoolName());
        hds.setMaxLifetime(dataBase1Properties.getMaxLifetime());
        hds.setConnectionTimeout(dataBase1Properties.getConnectionTimeout());
        hds.setConnectionTestQuery(dataBase1Properties.getConnectionTestQuery());
        return hds;
    }

    /**
     * 预安置人员数据录入中间库数据源
     * @return DataSource
     */
    @Bean(name = "shadowDataSource")
    public DataSource shadowDataSource() {
        HikariDataSource hds = new HikariDataSource();
        String str = dataBase4Properties.getPassword();
        byte[] bytes = DesCryptoGraphy.hexStringToByte(str);
        byte[] pwdb = DesCryptoGraphy.desCbcDecrypt(bytes,DesCryptoGraphy.KEY.getBytes());
        hds.setDriverClassName(dataBase4Properties.getDriverClassName());
        hds.setJdbcUrl(dataBase4Properties.getUrl());
        hds.setUsername(dataBase4Properties.getUsername());
        hds.setPassword(new String(Objects.requireNonNull(pwdb)));
        hds.setMinimumIdle(dataBase4Properties.getMinimumIdle());
        hds.setMaximumPoolSize(dataBase4Properties.getMaximumPoolSize());
        hds.setAutoCommit(dataBase4Properties.isAutoCommit());
        hds.setIdleTimeout(dataBase4Properties.getIdleTimeout());
        hds.setPoolName(dataBase4Properties.getPoolName());
        hds.setMaxLifetime(dataBase4Properties.getMaxLifetime());
        hds.setConnectionTimeout(dataBase4Properties.getConnectionTimeout());
        hds.setConnectionTestQuery(dataBase4Properties.getConnectionTestQuery());
        return hds;
    }

    /**
     * 部级数据库数据源
     * @return DataSource
     */
    @Bean(name = "bujiDataSource")
    public DataSource bujiDataSource() {
        HikariDataSource hds = new HikariDataSource();
        String str = dataBase2Properties.getPassword();
        byte[] bytes = DesCryptoGraphy.hexStringToByte(str);
        byte[] pwdb = DesCryptoGraphy.desCbcDecrypt(bytes,DesCryptoGraphy.KEY.getBytes());
        hds.setDriverClassName(dataBase2Properties.getDriverClassName());
        hds.setJdbcUrl(dataBase2Properties.getUrl());
        hds.setUsername(dataBase2Properties.getUsername());
        hds.setPassword(new String(Objects.requireNonNull(pwdb)));
        hds.setMinimumIdle(dataBase2Properties.getMinimumIdle());
        hds.setMaximumPoolSize(dataBase2Properties.getMaximumPoolSize());
        hds.setAutoCommit(dataBase2Properties.isAutoCommit());
        hds.setIdleTimeout(dataBase2Properties.getIdleTimeout());
        hds.setPoolName(dataBase2Properties.getPoolName());
        hds.setMaxLifetime(dataBase2Properties.getMaxLifetime());
        hds.setConnectionTimeout(dataBase2Properties.getConnectionTimeout());
        hds.setConnectionTestQuery(dataBase2Properties.getConnectionTestQuery());
        return hds;
    }

    /**
     * 自建数据库数据源
     * @return DataSource
     */
    @Bean(name = "zijianDataSource")
    public DataSource zijianDataSource() {
        HikariDataSource hds = new HikariDataSource();
        String str = dataBase5Properties.getPassword();
        byte[] bytes = DesCryptoGraphy.hexStringToByte(str);
        byte[] pwdb = DesCryptoGraphy.desCbcDecrypt(bytes,DesCryptoGraphy.KEY.getBytes());
        hds.setDriverClassName(dataBase5Properties.getDriverClassName());
        hds.setJdbcUrl(dataBase5Properties.getUrl());
        hds.setUsername(dataBase5Properties.getUsername());
        hds.setPassword(new String(Objects.requireNonNull(pwdb)));
        hds.setMinimumIdle(dataBase5Properties.getMinimumIdle());
        hds.setMaximumPoolSize(dataBase5Properties.getMaximumPoolSize());
        hds.setAutoCommit(dataBase5Properties.isAutoCommit());
        hds.setIdleTimeout(dataBase5Properties.getIdleTimeout());
        hds.setPoolName(dataBase5Properties.getPoolName());
        hds.setMaxLifetime(dataBase5Properties.getMaxLifetime());
        hds.setConnectionTimeout(dataBase5Properties.getConnectionTimeout());
        hds.setConnectionTestQuery(dataBase5Properties.getConnectionTestQuery());
        return hds;
    }

    /**
     * 预安置同步中间库数据源 - dm
     * @return DataSource
     */
    @Bean(name = "dmDataSource")
    public DataSource dmDataSource() {
        HikariDataSource hds = new HikariDataSource();
        hds.setDriverClassName(dataBase6Properties.getDriverClassName());
        hds.setJdbcUrl(dataBase6Properties.getUrl());
        hds.setUsername(dataBase6Properties.getUsername());
        hds.setPassword(dataBase6Properties.getPassword());
        hds.setMinimumIdle(dataBase6Properties.getMinimumIdle());
        hds.setMaximumPoolSize(dataBase6Properties.getMaximumPoolSize());
        hds.setAutoCommit(dataBase6Properties.isAutoCommit());
        hds.setIdleTimeout(dataBase6Properties.getIdleTimeout());
        hds.setPoolName(dataBase6Properties.getPoolName());
        hds.setMaxLifetime(dataBase6Properties.getMaxLifetime());
        hds.setConnectionTimeout(dataBase6Properties.getConnectionTimeout());
        hds.setConnectionTestQuery(dataBase6Properties.getConnectionTestQuery());
        return hds;
    }

    /**
     * 预安置同步中间库数据源 - oracle
     * @return DataSource
     */
    @Bean(name = "oracleDataSource")
    public DataSource oracleDataSource() {
        HikariDataSource hds = new HikariDataSource();
        hds.setDriverClassName(dataBase7Properties.getDriverClassName());
        hds.setJdbcUrl(dataBase7Properties.getUrl());
        hds.setUsername(dataBase7Properties.getUsername());
        hds.setPassword(dataBase7Properties.getPassword());
        hds.setMinimumIdle(dataBase7Properties.getMinimumIdle());
        hds.setMaximumPoolSize(dataBase7Properties.getMaximumPoolSize());
        hds.setAutoCommit(dataBase7Properties.isAutoCommit());
        hds.setIdleTimeout(dataBase7Properties.getIdleTimeout());
        hds.setPoolName(dataBase7Properties.getPoolName());
        hds.setMaxLifetime(dataBase7Properties.getMaxLifetime());
        hds.setConnectionTimeout(dataBase7Properties.getConnectionTimeout());
        return hds;
    }

    /**
     * 描述
     * @param dataSource dataSource
     * @return JdbcTemplate
     */
    @Bean
    public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 描述
     * @param dataSource dataSource
     * @return JdbcTemplate
     */
    @Bean(name = "middleJdbcTemplate")
    public JdbcTemplate middleJdbcTemplate(@Qualifier("middleDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 描述
     * @param dataSource dataSource
     * @return JdbcTemplate
     */
    @Bean(name = "shadowJdbcTemplate")
    public JdbcTemplate shadowJdbcTemplate(@Qualifier("shadowDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 描述
     * @param bujiDataSource bujiDataSource
     * @return JdbcTemplate
     */
    @Bean(name = "bujiJdbcTemplate")
    public JdbcTemplate bujiJdbcTemplate(@Qualifier("bujiDataSource") DataSource bujiDataSource) {
        return new JdbcTemplate(bujiDataSource);
    }

    /**
     * 描述
     * @param dataSource dataSource
     * @return JdbcTemplate
     */
    @Bean(name = "zijianJdbcTemplate")
    public JdbcTemplate zijianJdbcTemplate(@Qualifier("zijianDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 描述
     * @param dataSource dataSource
     * @return JdbcTemplate
     */
    @Bean(name = "dmJdbcTemplate")
    public JdbcTemplate dmJdbcTemplate(@Qualifier("dmDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 描述
     * @param dataSource dataSource
     * @return JdbcTemplate
     */
    @Bean(name = "oracleJdbcTemplate")
    public JdbcTemplate oracleJdbcTemplate(@Qualifier("oracleDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}